<?php

class m_code extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    //get all fileds
    function getAll() {
        $strSql = "SELECT * FROM com_code";
        $strQuery = $this->db->query($strSql);
        if ($strQuery->num_rows() > 0) {
            foreach ($strQuery->result_array() as $arrRow) {
                $arrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }

    //get all child fileds
    function getAllChild() {
        $strSql = "SELECT * FROM com_code WHERE code_group !='root'";
        $strQuery = $this->db->query($strSql);
        if ($strQuery->num_rows() > 0) {
            foreach ($strQuery->result_array() as $arrRow) {
                $arrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }

    //get all fileds
    function getAllCode($pArrParams) {
        $strSql = "SELECT * FROM com_code LIMIT ?,?";
        $strQuery = $this->db->query($strSql, $pArrParams);
        if ($strQuery->num_rows() > 0) {
            foreach ($strQuery->result_array() as $arrRow) {
                $arrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }

    //get code by id
    function getById($pStrComCd) {
        $strSql = "SELECT * FROM com_code WHERE com_cd =?";
        $strQuery = $this->db->query($strSql, $pStrComCd);
        if ($strQuery->num_rows() > 0) {
            $arrResult = $strQuery->row_array();
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }

    //get code by group
    function getCodeByGroup($pStrCodeGroup) {
        $strSql = "SELECT com_cd,code_nm FROM com_code WHERE code_group = ?";
        $strQuery = $this->db->query($strSql, $pStrCodeGroup);
        if ($strQuery->num_rows() > 0) {
            $arrResult = $strQuery->result_array();
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }

    //save fields data
    function saveAdd($pArrParams) {
        $boolStatus = FALSE;

        //cek duplikasi id
        $this->db->where('com_cd', $pArrParams[0]);
        $this->db->from('com_code');
        $count = $this->db->count_all_results();

        if ($count > 0) {
            $boolStatus = FALSE;
        } else {
            $strSql = "INSERT INTO com_code (com_cd, code_nm, code_group, code_value, modi_id, modi_datetime)
                   VALUES (?, ?, ?, ?, ?, NOW())";
            if ($this->db->query($strSql, $pArrParams)) {
                $boolStatus = TRUE;
            } else {
                $boolStatus = FALSE;
            }
        }
        return $boolStatus;
        //return $this->db->query($strSql, $params);
    }

    //update code
    function saveEdit($pArrParams) {
        $strSql = "UPDATE com_code SET code_nm=?, code_group=?, code_value=?, modi_id=?,  modi_datetime=NOW() WHERE com_cd=?";
        return $this->db->query($strSql, $pArrParams);
    }

    //delete code
    function delete($pStrComCd) {
        $strSql = "DELETE FROM com_code WHERE com_cd=?";
        return $this->db->query($strSql, $pStrComCd);
    }

    //count code
    function count() {
        return $this->db->count_all_results("com_code");
    }

}

?>
